package cn.edu.zut.dao;

import cn.edu.zut.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserDao {
    @Select("select u.*, d.name depName, s.name statusName from user u, dep d, status s where u.userName = #{userName} and u.status_id = s._id and u.department_id = d._id")
    @Results(value = {
            @Result(id = true, column = "_id", property = "_id"),
            @Result(column = "userName", property = "userName"),
            @Result(column = "passwd", property = "passwd"),
            @Result(column = "nickName", property = "nickName"),
            @Result(column = "gender", property = "gender"),
            @Result(column = "department_id", property = "dep._id"),
            @Result(column = "status_id", property = "status._id"),
            @Result(column = "depName", property = "dep.name"),
            @Result(column = "statusName", property = "status.name"),
            @Result(column = "_id", property = "contestRegistries", many = @Many(select = "cn.edu.zut.dao.ContestRegistryDao.findBySTNumberId", fetchType = FetchType.LAZY))
    })
    public User findByUserName(String userName);

    @Insert("insert into user(userName, passwd, nickName, gender, department_id, status_id) values(#{userName}, #{passwd}, #{nickName}, #{gender}, #{dep._id}, #{status._id})")
    public void insert(User user);

    @Update("update user set passwd = #{passwd}, nickName = #{nickName}, gender = #{gender} where userName = #{userName}")
    public void update(User user);

    @Select("select u.*, d.name depName, s.name statusName from user u, dep d, status s where u.status_id = s._id and u.department_id = d._id order by u._id desc;")
    @Results(value = {
            @Result(id = true, column = "_id", property = "_id"),
            @Result(column = "userName", property = "userName"),
            @Result(column = "passwd", property = "passwd"),
            @Result(column = "nickName", property = "nickName"),
            @Result(column = "gender", property = "gender"),
            @Result(column = "department_id", property = "dep._id"),
            @Result(column = "status_id", property = "status._id"),
            @Result(column = "depName", property = "dep.name"),
            @Result(column = "statusName", property = "status.name")
    })
    public List<User> findAll();
    @Select("select * from user")
    public List<User> findAllUsers();
    @Delete("delete from user where userName = #{userName}")
    public void deleteByUserName(String userName);
    //and login_name like '%'||#{login_name,jdbcType=VARCHAR}||'%'
    @Select("<script>" +
            "select u.*, d.name depName, s.name statusName from user u, dep d, status s where 1 = 1 and u.status_id = s._id and u.department_id = d._id" +
            "<if test=\"userName != null and userName != ''\">" +
            " and userName like #{userName}" +
            "</if>" +
            " order by u._id desc"
            + "</script>"
    )
    @Results(value = {
            @Result(id = true, column = "_id", property = "_id"),
            @Result(column = "userName", property = "userName"),
            @Result(column = "passwd", property = "passwd"),
            @Result(column = "nickName", property = "nickName"),
            @Result(column = "gender", property = "gender"),
            @Result(column = "department_id", property = "dep._id"),
            @Result(column = "status_id", property = "status._id"),
            @Result(column = "depName", property = "dep.name"),
            @Result(column = "statusName", property = "status.name")
    })
    public List<User> findByLikeUserName(User user);


    @Select("select u.*, d.name depName, s.name statusName from user u, dep d, status s where u.status_id = s._id and u.department_id = d._id order by u._id desc limit #{maxNumber} offset #{offset};")
    @Results(value = {
            @Result(id = true, column = "_id", property = "_id"),
            @Result(column = "userName", property = "userName"),
            @Result(column = "passwd", property = "passwd"),
            @Result(column = "nickName", property = "nickName"),
            @Result(column = "gender", property = "gender"),
            @Result(column = "department_id", property = "dep._id"),
            @Result(column = "status_id", property = "status._id"),
            @Result(column = "depName", property = "dep.name"),
            @Result(column = "statusName", property = "status.name")
    })
    public List<User> findPart(@Param("offset") int offset, @Param("maxNumber") int maxNumber);

}
